﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using Register.DBUtility;
using System.Data.SqlClient;
using System.Data;
using Register.Model;

namespace Register.DAL
{
    /// <summary>
    /// FileName: DoctorWorkDayDAL.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangKunLun
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/7/10 17:39:34
    /// </summary>
    public class DoctorWorkDayDAL : IDoctorWorkDayDAL
    {
        private string tableName = "System_doctorWorkDay";

        public int AddDoctorWorkDay(Model.DoctorWorkDay model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_doctorWorkDay(");
            strSql.Append("DoctorWorkDay_ID,Doctor_ID,DoctorWorkDay_TimeType,DoctorWorkDay_DayTime,DoctorWorkDay_AddTime,DoctorWorkDay_Recoder,DoctorWorkDay_State,DoctorWorkDay_Departments_ID)");
            strSql.Append(" values (");
            strSql.Append("@DoctorWorkDay_ID,@Doctor_ID,@DoctorWorkDay_TimeType,@DoctorWorkDay_DayTime,@DoctorWorkDay_AddTime,@DoctorWorkDay_Recoder,@DoctorWorkDay_State,@DoctorWorkDay_Departments_ID)");
            SqlParameter[] parameters = {
					new SqlParameter("@DoctorWorkDay_ID", SqlDbType.Char,14),
					new SqlParameter("@Doctor_ID", SqlDbType.Char,14),
					new SqlParameter("@DoctorWorkDay_TimeType", SqlDbType.Char,4),
					new SqlParameter("@DoctorWorkDay_DayTime", SqlDbType.DateTime),
					new SqlParameter("@DoctorWorkDay_AddTime", SqlDbType.DateTime),
					new SqlParameter("@DoctorWorkDay_Recoder", SqlDbType.VarChar,30),
					new SqlParameter("@DoctorWorkDay_State", SqlDbType.Char,1),
                    new SqlParameter("@DoctorWorkDay_Departments_ID", SqlDbType.Char,14)};
            parameters[0].Value = model.DoctorWorkDay_ID;
            parameters[1].Value = model.Doctor_ID;
            parameters[2].Value = model.DoctorWorkDay_TimeType;
            parameters[3].Value = model.DoctorWorkDay_DayTime;
            parameters[4].Value = model.DoctorWorkDay_AddTime;
            parameters[5].Value = model.DoctorWorkDay_Recoder;
            parameters[6].Value = model.DoctorWorkDay_State;
            parameters[7].Value = model.DoctorWorkDay_Departments_ID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }

        public int DeleteDoctorWorkDay(string DoctorWorkDayID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_doctorWorkDay set ");
            strSql.Append("DoctorWorkDay_State=0 ");
            strSql.Append(" where DoctorWorkDay_ID=@DoctorWorkDay_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DoctorWorkDay_ID", SqlDbType.Char,14)};
            parameters[0].Value = DoctorWorkDayID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        public int UpdateDoctorWorkDay(Model.DoctorWorkDay model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_doctorWorkDay set ");
            strSql.Append("DoctorWorkDay_TimeType=@DoctorWorkDay_TimeType,");
            strSql.Append("DoctorWorkDay_DayTime=@DoctorWorkDay_DayTime ");
            strSql.Append(" where DoctorWorkDay_ID=@DoctorWorkDay_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DoctorWorkDay_TimeType", SqlDbType.Char,4),
					new SqlParameter("@DoctorWorkDay_DayTime", SqlDbType.DateTime),
                    new SqlParameter("@DoctorWorkDay_ID", SqlDbType.Char,14)};
            parameters[0].Value = model.DoctorWorkDay_TimeType;
            parameters[1].Value = model.DoctorWorkDay_DayTime;
            parameters[2].Value = model.DoctorWorkDay_ID;
            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        public List<Model.DoctorWorkDay> GetPagedDoctorWorkDay(int size, int pageIndex, string where)
        {
            return DataSetToList(DbHelperSQL.getPagedData(" * ", " DoctorWorkDay_AddTime ", where, tableName, OrderBy.DESC, pageIndex, size));
        }

        //医生信息
        public List<Model.DoctorInfo> GetDoctorInfo()
        {
            String sql = " select Doctor_ID,Doctor_Name,Departments_ID from System_doctorInfo  where Doctor_State=1 ";

            return ChangeReaderToList(DbHelperSQL.ExecuteReader(sql));
        }

        private List<DoctorInfo> ChangeReaderToList(SqlDataReader reader)
        {
            List<DoctorInfo> list = new List<DoctorInfo>();
            while (reader.Read())
            {
                DoctorInfo doctorInfo = new DoctorInfo();
                doctorInfo.Doctor_ID = reader.GetString(0);
                doctorInfo.Doctor_Name = reader.GetString(1);
                doctorInfo.Departments_ID = reader.GetString(2);
                list.Add(doctorInfo);
            }
            return list;
        }


        public Model.DoctorWorkDay GetDoctorWorkDay(string DoctorWorkDayID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DoctorWorkDay_ID,Doctor_ID,DoctorWorkDay_TimeType,DoctorWorkDay_DayTime,DoctorWorkDay_AddTime,DoctorWorkDay_Recoder,DoctorWorkDay_State,DoctorWorkDay_Departments_ID from System_doctorWorkDay ");
            strSql.Append(" where DoctorWorkDay_ID=@DoctorWorkDay_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ADoctorWorkDay_ID", SqlDbType.Char,14)};
            parameters[0].Value = DoctorWorkDayID;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(), parameters)).First();
        }
        //根据科室获得信息
        public List<Model.DoctorWorkDay> GetDepartments_ID(string Departments_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  DoctorWorkDay_ID,Doctor_ID,DoctorWorkDay_TimeType,DoctorWorkDay_DayTime,DoctorWorkDay_AddTime,DoctorWorkDay_Recoder,DoctorWorkDay_State,DoctorWorkDay_Departments_ID from System_doctorWorkDay ");
            strSql.Append(" where DoctorWorkDay_State = 1 and DoctorWorkDay_Departments_ID=@DoctorWorkDay_Departments_ID  order by DoctorWorkDay_DayTime ");
            SqlParameter[] parameters = {
					new SqlParameter("@DoctorWorkDay_Departments_ID", SqlDbType.Char,14)};
            parameters[0].Value = Departments_ID;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(), parameters));
        }


        private List<Model.DoctorWorkDay> DataSetToList(DataSet ds)
        {
            List<Model.DoctorWorkDay> list = new List<Model.DoctorWorkDay>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DoctorWorkDay doctorWorkDay = new DoctorWorkDay();
                doctorWorkDay.DoctorWorkDay_ID = (String)dr["DoctorWorkDay_ID"];
                doctorWorkDay.Doctor_ID = (String)dr["Doctor_ID"];
                doctorWorkDay.DoctorWorkDay_TimeType = (String)dr["DoctorWorkDay_TimeType"];
                doctorWorkDay.DoctorWorkDay_DayTime = (DateTime)dr["DoctorWorkDay_DayTime"];
                doctorWorkDay.DoctorWorkDay_AddTime = (DateTime)dr["DoctorWorkDay_AddTime"];
                doctorWorkDay.DoctorWorkDay_Recoder = (String)dr["DoctorWorkDay_Recoder"];
                doctorWorkDay.DoctorWorkDay_State = (String)dr["DoctorWorkDay_State"];
                doctorWorkDay.DoctorWorkDay_Departments_ID = (String)dr["DoctorWorkDay_Departments_ID"];
                list.Add(doctorWorkDay);
            }
            return list;
        }

        public int GetDoctorWorkDayCount(string where)
        {
            int count = 0;
            StringBuilder str = new StringBuilder();
            str.Append(" select count(*) from ");
            str.Append(tableName);
            if (where != null)
            {
                str.Append(" where ");
                str.Append(where);
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            else
            {
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            return count;
        }
    }
}
